#include "CStringWriter.h"

namespace lampe{
namespace stream{

u32 CStringWriter::position(){
    return pos;
}

void CStringWriter::position(u32 p){
    if (p>=size())
        p=size()-1;
    else
        pos=p;
}

u32 CStringWriter::size(){
    return data.length();
}

u32 CStringWriter::write(void* buffer, u32 s){
    if (s==0)
        return 0;
    String inc((char*)buffer,s);

    if (position()==size()){
        data.append(inc);
        pos+=s;
        return s;
    }
    else{
        String res="";
        if (position()>0){
            res.append(data.substr(0,position()));
        }
        res.append(inc);

        if (size()-position()-1>s){
            res.append(data.substr(position()+s));
        }
        data=res;
    }
}


CStringWriter::CStringWriter(){
    data="";
    pos=0;
}

CStringWriter::~CStringWriter(){
    data="";
}

String CStringWriter::getData(){
    return data;
}

extern IStringWriter*createStringWriter(){
    return new CStringWriter();
}

}//namespace stream
}//namespace lampe
